use std::collections::HashMap;
use std::sync::LazyLock;

use parking_lot::RwLock;

use crate::{
    boxes::{ArrayViewBox, Complex64Box, StringBox},
    error::TonglunError,
    functions::TonglunFunction,
    pools::Pool,
    runtime::Runtime,
    values::ValueRef,
};

pub static POOL_RUNTIME: LazyLock<RwLock<Pool<Runtime>>> =
    LazyLock::new(|| RwLock::new(Pool::new()));

pub static POOL_STRINGS: LazyLock<RwLock<Pool<StringBox>>> =
    LazyLock::new(|| RwLock::new(Pool::new()));

pub static POOL_ARRAYS: LazyLock<RwLock<Pool<ArrayViewBox>>> =
    LazyLock::new(|| RwLock::new(Pool::new()));

pub static POOL_COMPLEX64: LazyLock<RwLock<Pool<Complex64Box>>> =
    LazyLock::new(|| RwLock::new(Pool::new()));

pub static POOL_TUPLES: LazyLock<RwLock<Pool<Vec<ValueRef>>>> =
    LazyLock::new(|| RwLock::new(Pool::new()));

pub static POOL_ERRORS: LazyLock<RwLock<Pool<TonglunError>>> =
    LazyLock::new(|| RwLock::new(Pool::new()));

/// 键通常是动态库的句柄，值为运行时 ID
type RuntimeId = u32;
pub static REGISTERED_RUNTIMES: LazyLock<RwLock<HashMap<usize, RuntimeId>>> =
    LazyLock::new(|| RwLock::new(HashMap::new()));

pub static REGISTERED_FUNCTIONS: LazyLock<dashmap::DashMap<String, TonglunFunction>> =
    LazyLock::new(|| dashmap::DashMap::new());
